from web import models
from django.db.models import Q
from utils.BootStrap import BootStrapModelForm


class wikiForm(BootStrapModelForm):
    class Meta:
        model = models.Wiki
        exclude = ['project', "depth"]

    def __init__(self, request, *args, **kwargs):
        super().__init__(*args, **kwargs)
        total_list = [(None, '请选择'), ]
        parent_old = self.instance.parent
        if self.instance:# 如果有self.instance则为编辑
            if parent_old:
               data_list = models.Wiki.objects.filter(~Q(parent=parent_old),~Q(id=parent_old.id), project=request.manage).values_list("id", 'title')
            else:
               data_list = models.Wiki.objects.filter(~Q(id=self.instance.id),~Q(parent=self.instance),project=request.manage).values_list("id",'title')
        # 没用则为添加
        data_list = models.Wiki.objects.filter(project=request.manage).values_list("id",'title')
        print(data_list)
        print(self.instance)
        total_list.extend(data_list)
        self.fields['parent'].choices = total_list
